package com.issoft.studentresitor.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import com.issoft.studentresitor.domain.dto.NoticeDTO;
import com.issoft.studentresitor.domain.pojo.Notice;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.query.NoticeQuery;
import com.issoft.studentresitor.domain.vo.NoticeVo;
import com.issoft.studentresitor.service.INoticeService;
import com.issoft.studentresitor.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author GJH
 * @since 2025-09-14
 */
@Api(tags = "通知公告管理")
@RestController
@RequestMapping("/admin/notice")
@CrossOrigin
public class NoticeController {
    @Autowired
    private INoticeService noticeService;
    /**
     * 分页查询通知公告
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result queryNoticePage(@RequestParam(required = false) String title,
                                  @RequestParam(required = false) String noticeType,
                                  @RequestParam(required = false) String publishState,
                                  @RequestParam(required = false) LocalDateTime publishTime,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pageSize
                                  ) {
        NoticeQuery query = new NoticeQuery();
        query.setTitle(title);
        query.setNoticeType(noticeType);
        query.setPublishState(publishState);
        query.setPublishTime(publishTime);
        query.setPage(page);
        query.setPageSize(pageSize);
        PageResult<NoticeVo> result = noticeService.queryNoticePage(query);
        return Result.success(result);
    }
    /**
     * 批量删除公告
     */
    @ApiOperation("批量删除公告")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> noticeIds) {
        boolean b = noticeService.removeByIds(noticeIds);
        return b ? Result.success("公告删除成功") : Result.error("批量删除失败");
    }
    /**
     * 添加公告
     */
    @ApiOperation("添加公告")
    @PostMapping("/add")
    public Result add(@RequestBody NoticeDTO noticeDTO) {
        Notice notice = new Notice();
        BeanUtil.copyProperties(noticeDTO, notice);
        boolean result = noticeService.save(notice);
        return result ? Result.success("添加成功") : Result.error("添加失败");
    }
    /**
     * 修改公告
     */
    @ApiOperation("修改公告")
    @PutMapping("/update")
    public Result update(@RequestBody NoticeDTO noticeDTO) {
        Notice notice = new Notice();
        BeanUtil.copyProperties(noticeDTO, notice);
        boolean result = noticeService.updateById(notice);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }
    /**
     * 根据id查询公告
     */
    @ApiOperation("根据id查询公告")
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id) {
        Notice notice = noticeService.getById(id);
        return notice != null ? Result.success(notice) : Result.error("公告不存在");
    }
    /**
     * 根据id删除
     */
    @ApiOperation("根据id删除")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean result = noticeService.removeById(id);
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }
}
